package net.csdra.libraryborrowing.service.user;

import net.csdra.libraryborrowing.domain.Message;
import net.csdra.libraryborrowing.domain.MessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.Assert;

/**
 * Created by add on 2017/10/17.
 */
public class UserMessageServiceImpl implements UserMessageService {
    @Autowired
    MessageRepository messageRepository;

    @Override
    public Message sendMessage(SendMessage sendMessage) {

        return null;
    }

    @Override
    public Page<Message> getMessagePage(GetMessagePage getMessagePage) {
        Assert.notNull(getMessagePage, "@参数不能为空");
        return this.messageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
            return  criteriaQuery.where(criteriaBuilder.equal(root.get("read"), getMessagePage.isRead())).getRestriction();
        }, new PageRequest(getMessagePage.getPage(), getMessagePage.getSize(), new Sort(Sort.Direction.DESC)));
    }
}
